﻿Imports System
Imports System.Drawing



Public Class ClassTriDatagridMulticolonnes

    Implements System.Collections.IComparer

    Private sortOrderModifier As Integer = 1
    Private _NumCol1, _NumCol2 As Integer
    ''' <summary>
    ''' invoque le tri personnalisé pour un datagridview
    ''' </summary>
    ''' <param name="sortOrder">ses du tri (sortOrder.Descending/Acending) </param>
    ''' <param name="NumCol1">1ère colonne de tri (colonne principale)</param>
    ''' <param name="NumCol2">2e colonne de tri</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal sortOrder As SortOrder, ByVal NumCol1 As Integer, ByVal NumCol2 As Integer)
        If sortOrder = sortOrder.Descending Then
            sortOrderModifier = -1
        ElseIf sortOrder = sortOrder.Ascending Then

            sortOrderModifier = 1
        End If
        _NumCol1 = NumCol1
        _NumCol2 = NumCol2
    End Sub

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
        Implements System.Collections.IComparer.Compare

        Dim DataGridViewRow1 As DataGridViewRow = CType(x, DataGridViewRow)
        Dim DataGridViewRow2 As DataGridViewRow = CType(y, DataGridViewRow)

        ' Try to sort based on the Last Name column.
        Dim CompareResult As Integer = System.String.Compare( _
            DataGridViewRow1.Cells(_NumCol1).Value.ToString(), _
            DataGridViewRow2.Cells(_NumCol1).Value.ToString())

        ' If the Last Names are equal, sort based on the First Name.
        If CompareResult = 0 Then
            CompareResult = System.String.Compare( _
                DataGridViewRow1.Cells(_NumCol2).Value.ToString(), _
                DataGridViewRow2.Cells(_NumCol2).Value.ToString())
        End If
        Return CompareResult * sortOrderModifier
    End Function
End Class
